<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Commodity</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content=""/>
    <meta name="author" content=""/>
    
<link href="../css/bootstrap.css" rel="stylesheet" />
<link href="../css/jquery-ui-1.8.21.custom.css" rel="stylesheet" />
<link href="../css/main.css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="../css/select2/select2.css">
<link rel="stylesheet" type="text/css" media="screen"
	href="../js/lib/poshytip/tip-darkgray/tip-darkgray.css" />

<script src="../js/lib/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="../js/lib/underscore-min.js" type="text/javascript"></script>
<script src="../js/lib/backbone-min.js" type="text/javascript"></script>
<script src="../js/lib/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>
<script src="../js/lib/mustache.js" type="text/javascript"></script>
<script src="../js/lib/bootstrap-dropdown.js" type="text/javascript"></script>
<!--script src="../js/lib/select2.min.js" type="text/javascript"></script-->
<script src="../js/lib/select2.js" type="text/javascript"></script>
<!--script src="../js/lib/poshytip/jquery.poshytip.min.js" type="text/javascript"></script-->
<script src="../js/lib/poshytip/jquery.poshytip.js" type="text/javascript"></script>
<script src="../js/lib/jquery.blockUI.js" type="text/javascript"></script>

<!-- views -->
<script src="../js/backbone-page.js" type="text/javascript"></script>
<script src="../js/backbone-form.js" type="text/javascript"></script>
<script src="../js/backbone-form-jui-ext.js" type="text/javascript"></script>
<script src="../js/backbone-table.js" type="text/javascript"></script>

<!-- templates -->
<script src="../js/templates/page-bootstrap.js" type="text/javascript"></script>
<script src="../js/templates/form-bootstrap.js" type="text/javascript"></script>
<script src="../js/templates/table-bootstrap.js" type="text/javascript"></script>

<!-- page -->
<script src="../js/page/commodity.js" type="text/javascript"></script>

<style>
td > input[type="text"]{
	width:80px;
}

textarea{
	width:100px;
}
</style>
<!--[if IE 7]> 
<style>
.alpha{
	margin-top:8px;
}
</style>
<![endif]-->

</head>
<body>	
	<input type="hidden" id="cur_page" name="cur_page" value=""/>	
	<input type="hidden" id="ndaId" name="ndaId" value=""/><!-- 83665950 -->

	<div class="subnav">
			<ul class="nav nav-pills">
				<li><a href="#sec_core">Quote</a></li>
				<li><a href="#sec_ladder">Ladder</a></li>
				<li><a href="#sec_name">Name</a></li>
				<li><a href="#sec_identifier">Identifier</a></li>
				<li><a href="#sec_clsSchema">Classification</a></li>
				<li><a href="#sec_sedol">SEDOL</a></li>
				<li><a href="#sec_refData">Reference Data</a></li>
				<li><a href="#sec_crsUnit">Cross Unit Conversion</a></li>
				<li><a href="#sec_assetRelationship">Asset Relationship</a></li>
				<li><a href="#sec_locations">Locations</a></li>
				<li><a href="#sec_miscellaneous">Miscellaneous</a></li>
			</ul>
	</div>

	<div style="margin-top:40px;">
		<input type="button" id="ro" value="activate"/>
		<input type="button" id="submit_button" value="submit_button"/>
	</div>
		
	<div id="main_content" style="margin-top:50px;"></div>
	
	<script>
	
	var page_readOnly = false;
	    
$(function() {	

		var Content = Backbone.Model.extend({
		    schema: commodity.sections
		});
		
		var con = new Content();
		var asset = new Backbone.Asset({
		    model: con,
		    readOnly: page_readOnly
		}).render();
		
		$("#main_content").append(asset.el);
				
		$("#ro").click(function(){
			if(page_readOnly){
				asset.activate();
			}
		});
		
		var s = "TESTCO01";
		$.ajax({
			url : "../../rest/asset/get",
			type : "POST",
			dataType : "json",
			data : {
				condition : '{"ASSET.RIC":[{"value":"' + s + '"}]}',
				properties : "ASSET.RIC,ASSET.TYPE,ASSET.ASSETCATEGORY,ASSET.ASSETSTATE,ASSET.RETIREDATE,ASSET.QUOTATIONCURRENCY,ASSET.TRADES,ASSET.QUOTATIONGROUPEDBYPSEUDOEXCH,name,classificationscheme",
				//result : "{\"flag\":0,\"properties\":{\"ASSET.BTUPERBARREL\": [{\"value\": \"112.22\"}],\"ASSET.EXCHANGE\": [{\"value\": \"test\"}],\"ASSET.ASSETCATEGORY\": [{\"value\": \"BFU\", \"meaning\": \"Bond Future\"}], \"ASSET.RETIREDATE\": [{\"value\": \"2001-06-11 00:00:00\"}], \"ASSET.BASECURRENCY\": [], \"ASSET.TRADES\": [{\"value\": \"492\", \"begin_date\": \"1990-04-15 01:00:00\"}], \"BODY_GROUP.SOURCETYPE\": [{\"value\": \"492\", \"begin_date\": \"1990-04-15 01:00:00\"}], \"BODY_GROUP.COMMONNAME\": [{\"value\": \"492\", \"begin_date\": \"1990-04-15 01:00:00\"}], \"ASSET.RELATEDNEWS\": [{\"value\": \"G:1\", \"meaning\": \"Emerging Market Countries\", \"begin_date\": \"2011-02-21 10:13:41\"}, {\"value\": \"B:104\", \"meaning\": \"Computer & Electronics Retailers (TRBC)\", \"begin_date\": \"2011-02-21 10:13:41\"}], \"name\": [{\"property\": \"common name\", \"value\": \"JP JGB\", \"begin_date\": \"1990-04-15 01:00:00\"}, {\"property\": \"short name\", \"value\": \"10YR JGB JUN01\", \"begin_date\": \"1990-04-15 01:00:00\"}, {\"property\": \"search display name\", \"value\": \"10YR JGB JUN01\", \"begin_date\": \"1990-04-15 01:00:00\"} ]}}"
				//result: "{\"flag\":0,\"properties\":{\"NAME\":[],\"ASSET.TRADES\":[{\"value\":\"179260\",\"begin_date\":\"2002-03-19 00:00:00\"}],\"ASSET.BASECURRENCY\":[],\"ASSET.TYPE\":[{\"value\":\"EQUITY\"}],\"ASSET.RIC\":[{\"value\":\"ZENX.NS\",\"begin_date\":\"2002-03-19 00:00:00\"}],\"ASSET.RETIREDATE\":[],\"ASSET.ASSETCATEGORY\":[{\"value\":\"ORD\",\"meaning\":\"Ordinary\"}],\"ASSET.EXCHANGE\":[]}}"
				result: "{\"flag\":0,\"properties\":{\"ASSET.RCSUNDERLYINGPRODUCT\":[{\"value\":\"46627750\",\"begin_date\":\"2012-09-01 10:46:14\"}],\"ASSET.LSEOPOL\":[{\"value\":\"XAFR\",\"meaning\":\"ALTERNATIVA FRANCE\",\"begin_date\":\"2012-09-03 05:28:53\"}],\"COMMODITY.BTUPERBARREL\":[{\"value\":\"100\"}],\"CLASSIFICATIONSCHEME\":[{\"value\":\"NMJE\",\"meaning\":\"NMJE\",\"begin_date\":\"2012-08-31 02:51:52\",\"type\":\"ASSET.ASSETROOTMAP\",\"displayName\":\"ASSET ROOT MAP\"},{\"value\":\"Y\",\"meaning\":\"Yes\",\"begin_date\":\"2012-09-03 05:29:15\",\"type\":\"ASSET.POL\",\"displayName\":\"POL\"},{\"value\":\"OPT\",\"meaning\":\"Option\",\"begin_date\":\"2012-09-03 05:42:01\",\"type\":\"ASSET.RCSASSETCLASS\",\"displayName\":\"RCS ASSET CLASS\"},{\"value\":\"3\",\"meaning\":\"Lowest priority entities, order not important.\",\"begin_date\":\"2012-08-30 05:03:45\",\"type\":\"ASSET.SEARCHRANKING\",\"displayName\":\"SEARCH RANKING\"}],\"ASSET.RIC\":[{\"value\":\"TESTCO01\",\"begin_date\":\"2012-08-30 05:03:45\"}],\"ASSET.RETIREDATE\":[{\"value\":\"01-MAR-14\"}],\"ASSET.SEDOL\":[{\"value\":\"4567653\",\"begin_date\":\"2012-09-03 05:20:47\"}],\"ASSET.PRIMARYCHAIN.RIC\":[{\"value\":\"0#LCO:\"}],\"LOCATIONS\":[{\"value\":\"AAR\",\"meaning\":\"Aarhus\",\"id\":388,\"type\":\"ASSET.PRODUCTIONLOCATION\",\"displayName\":\"PRODUCTION LOCATION\"},{\"value\":\"AAB\",\"meaning\":\"Arrabury QL\",\"id\":384,\"type\":\"ASSET.DELIVERYLOCATION\",\"displayName\":\"DELIVERY LOCATION\"}],\"ASSET.TRADES.COMMONNAME\":[{\"value\":\"Burgundy AB\"}],\"ASSET.RELATEDPAGE.RIC\":[{\"value\":\"ADD.L\"}],\"ASSET.UNDERLYINGASSET.CATEGORY\":[{\"value\":\"Alfalfa Hay\",\"meaning\":\"Alfalfa Hay\",\"id\":2784}],\"ASSET.PRIMARYCHAIN\":[{\"value\":\"67641787\",\"begin_date\":\"2012-09-01 08:46:22\"}],\"ASSET.RATINGASSET\":[{\"value\":\"Y\"}],\"TICKLADDER\":[{\"not_applicable_flag\":\"N\",\"lhe_references_nda_id\":9,\"name\":\"TICK LADDER D\",\"effective_from\":\"08-AUG-12\",\"effective_to\":\"19-SEP-12\",\"cla_price_type_nda_id\":2000070107,\"value\":\"ORDER\",\"meaning\":\"Order price\",\"begin_date\":\"01-SEP-12\"}],\"CRSUNIT\":[{\"id\":2000069955,\"value\":\"D\",\"meaning\":\"Wet Volume To Energy\",\"factor\":\"1.23\"},{\"id\":2000069959,\"value\":\"F\",\"meaning\":\"Dry Volume to Wet Volume\",\"factor\":\".4\"}],\"ASSET.ASSETSTATE\":[{\"value\":\"AC\",\"meaning\":\"Active\",\"id\":41}],\"NAME\":[{\"value\":\"10\",\"begin_date\":\"2012-08-31 09:48:14\",\"type\":\"ASSET.BRIDGEVEHICLE\",\"displayName\":\"BRIDGE VEHICLE\"},{\"value\":\"TEST Common name\",\"begin_date\":\"2012-08-30 12:01:37\",\"type\":\"ASSET.COMMONNAME\",\"displayName\":\"COMMON NAME\"},{\"value\":\"search display name\",\"begin_date\":\"2012-09-01 08:49:34\",\"type\":\"ASSET.SEARCHDISPLAYNAME\",\"displayName\":\"SEARCH DISPLAY NAME\"}],\"ASSET.TRADES\":[{\"value\":\"15882580\",\"begin_date\":\"2012-08-30 11:47:40\"}],\"COMMODITY.PRICINGUNIT\":[{\"value\":\"0.1 TONNES\",\"meaning\":\"0.1 TONNES\",\"id\":1000033100}],\"ASSET.TYPE\":[{\"value\":\"COMMODITY\"}],\"COMMODITY.BARRELSPERTON\":[{\"value\":\"6\"}],\"IDENTIFIER\":[{\"value\":\"TEST_VALUE_10\",\"begin_date\":\"2012-08-31 09:54:59\",\"type\":\"ASSET.HELSINKISYMBOL\",\"displayName\":\"HELSINKI SYMBOL\"},{\"value\":\"123\",\"begin_date\":\"2012-09-01 09:01:46\",\"type\":\"ASSET.ATHENSCODE\",\"displayName\":\"ATHENS CODE\"}],\"ASSET.DISPLAYTYPE\":[{\"value\":\"COMPGE\",\"meaning\":\"Market Price Summary Composite Page\",\"id\":2000053894}],\"LOTLADDER\":[{\"not_applicable_flag\":\"N\",\"lhe_references_nda_id\":17,\"name\":\"LOT LADDER E\",\"effective_from\":\"03-SEP-12\",\"effective_to\":\"21-SEP-12\",\"cla_price_type_nda_id\":2000070109,\"value\":\"CLOSE\",\"meaning\":\"Close price\",\"begin_date\":\"01-SEP-12\"}],\"ASSET.UNDERLYINGASSET\":[{\"value\":\"72\",\"begin_date\":\"2012-09-01 10:37:59\"}],\"ASSET.QUOTATIONGROUPEDBYPSEUDOEXCH\":[{\"value\":\"BUR\",\"meaning\":\"Burgundy\",\"begin_date\":\"2012-08-30 11:47:40\"}],\"ASSET.ASSETCATEGORY\":[{\"value\":\"CCS\",\"meaning\":\"Commodity Cash\",\"id\":2124}],\"ASSET.QUOTATIONCURRENCY\":[{\"value\":\"CAD\",\"meaning\":\"Canadian Dollar\",\"begin_date\":\"2012-08-30 07:13:39\"}],\"ASSET.MIC\":[{\"value\":\"XAFR\",\"meaning\":\"ALTERNATIVA FRANCE\",\"begin_date\":\"2012-09-03 05:29:15\"}]}}"
			},
			error : function(xhr, ajaxOptions, thrownError) {
				$.unblockUI();
				alert("error:"+thrownError);
				return false;
			},
			success : function(data) {
				if(data.flag == 0){
					var props = data.properties, forms = asset.getForms(), tables = asset.getTables();
					
					if(forms){
						var keys = _.keys(forms);
						_.each(keys, function(key){
							forms[key].setValue(props, tables);
						});
					}
					
					if(tables){
						var keys = _.keys(tables);
						_.each(keys, function(key){
							if(props[key]){
								tables[key].setValue(props[key]);
							}else{
								tables[key].setValue([]);
							}
						});
					}

				}else if(data.flag == 1){
					alert(data.error.info);
				}else{
					alert("results format error");
				}
				//$.unblockUI();
				return false;
			}
		});
		
		$("#submit_button").click(function(){
			var forms = asset.getForms(), tables = asset.getTables(), data = [], flag = true, keys, finalResult;
			if(forms){
				keys = _.keys(forms);
				_.each(keys, function(key){
					var result = forms[key].commit();
					if(result.flag != "success"){
						result.flag = false;
						return true;
					}
				});
				if(flag){
					//initial properties array of every form
					var prop_arr = {};
					_.each(keys, function(key){
						prop_arr[key] = [];
					});
					
					//construct data(json format)
					_.each(modified, function(prop){
						var back = prop.back, type = prop.type, value = $("#"+prop.id).val(), operation = "";
						if(type === "Select"){
							var o = $("#"+prop.id).parent().parent().find(".btn-group").find("button:first");
							if(o.length > 0){						
								operation = ', "operation": "' + o.text() + '"';
							}						
						}else if(type === "Date"){
							var date = $("#"+prop.id).datepicker('getDate');
							value = $.datepicker.formatDate("yy-mm-dd 00:00:00", date);
						}
						data.push('"' + back + '":[{"value":"' + value + '"' + operation + '}]');	
						prop_arr[prop.form].push(back);
					});
					console.log(data.join(","));
					
					//after save successfully, set current_value as old_value 
					_.each(keys, function(key){
						forms[key].restore(prop_arr[key]);
					});
					//empty modified
					modified = [];
				}
			}

			if(flag){
				if(tables){
					var keys = _.keys(tables);
					_.each(keys, function(key){
						var val = tables[key].getValue();
						if(val.length > 0){
							data.push('"' + key + '":[' + val.join(",") + ']');
							console.log('"' + key + '":[' + val.join(",") + ']');
						}
					});
				}
			}			

			console.log(data.join(","));
			
	        //save data
	        /*$.ajax({
				url : "../../rest/asset/saveAsset",
				type : "POST",
				dataType : "json",q
				data : {
					"asset" : "{" + detail.join(",") + "}"
				},
				error : function(xhr, ajaxOptions, thrownError) {
					alert(thrownError);
					return false;
				},
				success : function(data) {
					alert(data.flag);
					form.restore(result.keys);
					return false;
				}
			});		*/
		});
		
		// fix sub nav on scroll
	    var $win = $(window)
	      , $nav = $('.subnav')
	      , navTop = $('.subnav').length && $('.subnav').offset().top - 40
	      , isFixed = 0;
	    
	    function processScroll() {
		      var i, scrollTop = $win.scrollTop();
		      if (scrollTop >= navTop && !isFixed) {
		        isFixed = 1;
		        $nav.addClass('subnav-fixed');
		      } else if (scrollTop <= navTop && isFixed) {
		        isFixed = 0;
		        $nav.removeClass('subnav-fixed');
		      }
		}

	    processScroll();

	    // hack sad times - holdover until rewrite for 2.1
	    $nav.on('click', function () {
	      if (!isFixed) setTimeout(function () {  $win.scrollTop($win.scrollTop() - 47); }, 10);
	    });

	    $win.on('scroll', processScroll);
		
});

</script>

	
</body>
</html>